/*****************************************************\
* Copyright (c) 2020 The game2d Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.

* Author:         dzlua - https://gitee.com/dzlua/game2d.git
* Email:          505544956@qq.com
* FileName:       ctrls/control.hpp
* Create Time:    2020/03/18 19:11:47
* Description:    /
\****************************************************/
#ifndef __GAME2D_CTRLS_CONTROL_H__
#define __GAME2D_CTRLS_CONTROL_H__

#include "core/sprite.h"
#include "core/builder.h"

namespace g2d {
namespace ctrl {
  enum class HorAlign : Uint8 {
    Left, Center, Right
  };
  enum class VerAlign : Uint8 {
    Top, Middle, Bottom
  };
  struct Inset {
    float l,t,r,b;
    Inset() : l(0.f), t(0.f), r(0.f), b(0.f) {}
  };
  
  class Control : public Sprite {
    public:
      Control(bool load_res_on_enter);
      virtual ~Control() = default;
    public:
      virtual Control* parent_ctrl() const;
      virtual Control* item_ctrl(Uint32 idx) const;
    public:
      virtual Size test_size() const;
    public:
      virtual void set_fixed_size(float w, float h);
      virtual void set_fixed_size(const Size &size);
      virtual Size fixed_size() const;

      virtual void set_disabled_image(TextureData *image);
      virtual const TextureData& disabled_image() const;
    public:
      virtual void set_attribute(const char *name, const char *value);
    public:
      virtual void update(Input &inp, Audio &aud) override;
      virtual void render(g2d::Renderer &r) override;
    public:
      virtual void update_control(Input &inp, Audio &aud);
      virtual void update_layout();
      virtual void render_color(g2d::Renderer &r);
      virtual void render_image(g2d::Renderer &r);
      virtual void render_text(g2d::Renderer &r);
      virtual void render_border(g2d::Renderer &r);
    protected:
      Size fixed_size_;
      TextureData disabled_image_;
  };

  class Label : public Control {
    public:
      Label(bool load_res_on_enter);
      virtual ~Label() = default;
    public:
      virtual void set_font(const char *id);
      virtual const char *font() const;
      virtual void set_text(const char *text);
      virtual const char* text() const;
      virtual void set_text_color(const SDL_Color &color);
      virtual const SDL_Color& text_color() const;
      virtual void set_disabled_text_color(const SDL_Color &color);
      virtual const SDL_Color& disabled_text_color() const;

      virtual void set_text_halign(HorAlign align);
      virtual HorAlign text_halign() const;
      virtual void set_text_valign(VerAlign align);
      virtual VerAlign text_valign() const;

      virtual void set_text_padding(const SDL_FPoint &padding);
      virtual const SDL_FPoint& text_padding() const;
    public:
      virtual Size test_size() const override;
      virtual void set_attribute(const char *name, const char *value) override;
    public:
      virtual void render_text(g2d::Renderer &r) override;
      SDL_FRect calculate_text_rect(g2d::Renderer &r);
    protected:
      std::string font_;
      std::string text_;
      SDL_Color text_color_;
      SDL_Color disabled_text_color_;
      HorAlign text_halign_;
      VerAlign text_valign_;
      SDL_FPoint text_padding_;
  };

  class FpsLabel : public Label {
    public:
      FpsLabel(bool load_res_on_enter) : Label(load_res_on_enter) {}
      virtual ~FpsLabel() = default;
    protected:
      virtual void update_control(Input &inp, Audio &aud) override {
        text_ = std::to_string(g2d_->real_fps());
      }
  };

  class Button : public Label {
    public:
      Button(bool load_res_on_enter);
      virtual ~Button() = default;
    public:
      virtual void set_hot_text_color(const SDL_Color &color);
      virtual const SDL_Color& hot_text_color() const;
      virtual void set_pushed_text_color(const SDL_Color &color);
      virtual const SDL_Color& pushed_text_color() const;
      virtual void set_hot_image(TextureData *image);
      virtual const TextureData& hot_image() const;
      virtual void set_pushed_image(TextureData *image);
      virtual const TextureData& pushed_image() const;
    public:
      virtual void set_attribute(const char *name, const char *value) override;
      virtual void update_control(Input &inp, Audio &aud) override;
      virtual void render_image(g2d::Renderer &r) override;
      virtual void render_text(g2d::Renderer &r) override;
    protected:
      SDL_Color hot_text_color_;
      SDL_Color pushed_text_color_;
      TextureData hot_image_;
      TextureData pushed_image_;
      bool hot_;
      bool pressed_;
  };
} // end namespace ctrl
} // end namespace g2d

#endif // __GAME2D_CTRLS_CONTROL_H__